{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b0af2913",
   "metadata": {},
   "source": [
    "用for语句一行一行堆叠产生一个5*5的数组ar，每一行arange(i*10,i*10+5)，共5行。再用for语句对每一行打乱数据元素。将ar存入”ar.txt”文件中。将ar按列排序为ar2，行不变。再次存入ar.txt中，观察文件中的数据是否改变。对ar按行打乱，存入ar.txt，观察。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d5da333a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7de34c2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "ar = np.arange(0,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3a93dc09",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1,5):\n",
    "    arr0 = np.arange(i*10,i*10+5)\n",
    "    ar = np.vstack((ar,arr0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "89fae160",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [20, 21, 22, 23, 24],\n",
       "       [30, 31, 32, 33, 34],\n",
       "       [40, 41, 42, 43, 44]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a5ec2f5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(5):\n",
    "    np.random.shuffle(ar[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7059ea0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  1,  3,  0],\n",
       "       [10, 13, 11, 12, 14],\n",
       "       [22, 24, 23, 20, 21],\n",
       "       [32, 33, 34, 31, 30],\n",
       "       [43, 44, 42, 41, 40]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "09f55e4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt(\"ar.txt\",ar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d560d66f",
   "metadata": {},
   "outputs": [],
   "source": [
    "ar2 = np.sort(ar,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a3cf85c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt(\"ar.txt\",ar2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "129da78b",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.shuffle(ar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6fd4a117",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 13, 11, 12, 14],\n",
       "       [32, 33, 34, 31, 30],\n",
       "       [22, 24, 23, 20, 21],\n",
       "       [ 2,  4,  1,  3,  0],\n",
       "       [43, 44, 42, 41, 40]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ar"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
